/**
 *机器试妆参数管理视图
 *@Author: zfx
 *@Date 2018-07-02 9:30:00
 */
<style lang="less" rel="stylesheet/less" type="text/less">
    .v-admin-machine-parameters-views {
        padding: 20px;
    }
</style>
<template>
	<div class="v-admin-machine-parameters-views">
		<Tabs
			type="card"
			@on-click="changeTab">

			<TabPane
				:label="$t('common.create')"
				name="新增">
				<machine-create
					ref="create"
					:loading="table_loading"
					:machines-info="machines_info"
					@post="postData"/>
			</TabPane>

			<TabPane
				:label="$t('common.check')"
				name="查看">
				<machine-index
					:loading="table_loading"
					:machines="machines.data"
					:machines-info="machines_info"
					@edit="machineDetail"
					@delete="deleteMachine"/>
				<br>

				<!-- 分页组件 -->
				<Row
					type="flex"
					justify="center">
					<Page
						:total="machines.total"
						:current="machines.current_page"
						:page-size="query_info.per_page"
						placement="top"
						show-sizer
						show-total
						@on-change="pageChange"
						@on-page-size-change="perPageChange"/>
				</Row>

			</TabPane>

		</Tabs>

		<Modal
			v-model="show_modal"
			:title="$t('machine.machine_test')"
			@on-cancel="closeModal">

			<!-- 机器试妆参数编辑组件 -->
			<machine-edit
				ref="edit"
				:machine-detail="machine_detail"
				:machines-info="machines_info"
				:loading="table_loading"
				@update="updateMachine"/>

			<div slot="footer"/>

		</Modal>

	</div>
</template>

<script>

import API_LIST from 'src/API_LIST';

import { mapMutations } from 'vuex';

import qs from 'qs';

import machineCreate from './create';

import machineIndex from './index';

import machineEdit from './edit';

export default {

	name: 'MachineParametersViews',

	components: {
		machineCreate,

		machineIndex,

		machineEdit,
	},
	data() {
		return {

			table_loading: false, // 表格更新时的loading

			show_modal: false, // 弹框的控制

			machine_devices: {}, // 机器设备相关信息

			machine_detail: {}, // 机器设备详情

			machines: {}, // 机器列表

			machines_info: [], // 机器试妆参数信息

			query_info: {
				page: 1,

				per_page: 10,

				sort_column: '',

				sort_by: '',
			},

			search_info: {

				page: 1,

			},

		};
	},
	created() {
		this.fetchMachineCreate();
	},
	methods: {

		...mapMutations('spinShow', [
			'showSpin',
			'closeSpin',
		]),

		/**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined}
         */
		changeTab(tab_name) {
			if (tab_name === '查看') {
				this.fetchMachineList(this.query_info);
			} else {
				this.machines.data = [];
				this.$refs.create.handleReset('formValidate');
			}
		},

		/**
         * 新增机器试妆参数的post数据
		 * @param {Object} param {新增的参数}
         * @return {undefined}
         */
		postData(param) {
			this.showSpin(this.$t('common.please_wait'));

			this.$http.post(API_LIST.MACHINE, param)

				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.create_success'));

					this.$refs.create.handleReset('formValidate');
				})

				.catch((err) => {
					this.$Message.error(err.message);
				})

				.then(this.closeSpin);
		},

		/**
         * 获取机器试妆参数列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
		fetchMachineList(param) {
			this.table_loading = true;

			const query_str = qs.stringify(param);

			this.$http.get(`${API_LIST.MACHINE}?${query_str}`)

				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);
					this.machines = data.device_specs;
				})

				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})

				.then(() => { this.table_loading = false; });
		},

		/**
		 * 获取机器试妆参数信息
		 * @return {undefined}
		 */
		fetchMachineCreate() {
			this.showSpin();

			this.$http.get(`${API_LIST.MACHINE}/create`)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);
					this.machines_info = data.device_spec;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},

		/**
		 * 关闭模态框
		 * @return {undefined}
		 */
		closeModal() {
			this.machine_detail = {};
		},

		/**
         * 查看机器试妆参数列表详情
         * @param  {string} id {商品详情id}
         * @return {undefined}
         */
		machineDetail(detail) {
			this.machine_detail = detail;

			this.show_modal = true;
		},

		/**
         * 删除机器试妆参数
         * @param  {string} id {需要删除的banner的id}
         * @return {undefined}
         */
		deleteMachine(id) {
			this.$Modal.confirm({

				title: this.$t('product.tip'), // '温馨提示',
				content: this.$t('product.is_delete'), // '删除之后将无法恢复，确定吗？',

				onOk: () => {
					const param = {

						_method: 'DELETE',

					};

					this.table_loading = true;

					this.$http.post(`${API_LIST.MACHINE}/${id}`, param)

						.then(({ data }) => {
							if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

							this.fetchMachineList(this.search_info);

							this.$Message.success(this.$t('common.delete_success'));
						})

						.catch((err) => {
							this.$Message.error(err.message || err.statusText);
						})

						.then(() => { this.table_loading = false; });
				},

				onCancel: () => {},

			});
		},

		/**
         * 页码改变
         * @param  {string} pageNumber {当前页码}
         * @return {undefined}
         */
		pageChange(pageNumber) {
			this.search_info.page = pageNumber;

			this.search_info.per_page = this.query_info.per_page;

			this.fetchMachineList(this.search_info);
		},

		/**
         * 每页显示数改变
         * @param  {string} per_page {当前每页显示数}
         * @return {undefined}
         */
		perPageChange(per_page) {
			this.search_info.per_page = per_page;

			this.query_info.per_page = per_page;

			this.fetchMachineList(this.query_info);
		},

		/**
         * 更新机器试妆参数列表信息
         * @param  {Object} param {需要更新的参数对象}
         * @return {undefined}
         */
		updateMachine(id, param) {
			this.showSpin();

			this.$http.post(`${API_LIST.MACHINE}/${id}`, param)

				.then(({ data }) => {
					if (parseInt(data.code, 10) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.update_success'));

					this.show_modal = false;

					this.fetchMachineList(this.search_info);
				})

				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})

				.then(this.closeSpin);
		},

	},
};
</script>

